<template>
  <div>
    <div>
      <el-input
        size="small"
        class="addPosInput"
        placeholder="添加职称"
        :clearable="true"
        v-model.trim="jl.name"
      >
      </el-input>

      <el-select v-model="jl.titleLevel" placeholder="职称等级" size="small">
        <el-option
          v-for="item in titleLevels"
          :key="item"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-edit"
        style="margin-left: 10px"
        @click="addtitleLevel"
        >添加</el-button
      >
    </div>
    <div>
      <el-table
        :data="jls"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="id" width="100"> </el-table-column>
        <el-table-column prop="name" label="职称名" width="180">
        </el-table-column>
        <el-table-column prop="titleLevel" label="职称等级"> </el-table-column>
        <el-table-column prop="createDate" label="日期"> </el-table-column>
        <el-table-column prop="enabled" label="启用">
          <template slot-scope="scope">
            <el-tag size="mini" type="success" v-if="scope.row.enabled"
              >已启用</el-tag
            >
            <el-tag size="mini" type="danger" v-else>未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="showEditView(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="deleteJl(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职称名称</el-tag>
          </td>
          <td>
            <el-input size="small" v-model="updateJl.name"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职称等级</el-tag>
          </td>
          <td>
            <el-select
              v-model="updateJl.titleLevel"
              placeholder="职称等级"
              size="small"
            >
              <el-option
                v-for="item in titleLevels"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
              v-model="updateJl.enabled"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="已启用"
              inactive-text="未启用"
            >
            </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
    <el-button type="danger" @click="deleteMany" :disabled="multipleSelection.length==0">批量删除</el-button>
  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data() {
    return {
      jl: {
        name: "",
        titleLevel: "",
      },
      titleLevels: ["正高级", "副高级", "中级", "初级", "员级"],
      jls: [],
      dialogVisible: false,
      updateJl: {
        name: "",
        titleLevel: "",
        enabled: false,
      },
      multipleSelection: []
    };
  },
  methods: {
    //初始化数据表
    initJls() {
      this.getRequest("/system/basic/joblevel/").then((resp) => {
        if (resp) {
          this.jls = resp;
          this.jl.name = "";
          this.jl.titleLevel = "";
        }
      });
    },
    //添加职称
    addtitleLevel() {
      if (this.jl.name && this.jl.titleLevel) {
        console.log(this.jl);
        this.postRequest("/system/basic/joblevel/", this.jl).then((resp) => {
          console.log(resp);
          if (resp) {
            console.log(resp);
            this.initJls();
          }
        });
      } else {
        this.$message.error("职称名称不能为空");
      }
    },
    //编辑框的弹出
    showEditView(data) {
      Object.assign(this.updateJl, data);
      //后台数据库不接受createDate这个参数所以设置为空
      this.updateJl.createDate = "";
      this.dialogVisible = true;
    },
    //更新操作
    doUpdate() {
      this.putRequest("/system/basic/joblevel/", this.updateJl).then((resp) => {
        if (resp) {
          this.initJls();
          this.dialogVisible = false;
        }
      });
    },
    // 删除操作
    deleteJl(data) {
      this.$confirm("此操作将永久删除 " + data.name + " 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteRequest("/system/basic/joblevel/" + data.id).then(
            (resp) => {
              if (resp) {
                this.initJls();
              }
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteMany() {
      this.$confirm("此操作将永久删除 " + this.multipleSelection.length + " 条信息 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = '?'
          this.multipleSelection.forEach(item=>{
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest("/system/basic/joblevel/" + ids).then((resp) => {
            if (resp) {
              this.initJls();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //多选框
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    //切换列表颜色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return "warning-row";
      } else if (rowIndex % 2 == 1) {
        return "success-row";
      }
      return "";
    },
  },
  mounted() {
    this.initJls();
  },
};
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>